//
//  HSPShopperViewController.m
//  Icar
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "HSPShopperViewController.h"
#import "BaseHeader.h"
#import "HSPriceCollectionViewCell.h"
#import "AFNBlock.h"
#import "HSPpriceMoneyViewController.h"
@interface HSPShopperViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UITableView *tableViewShopper;
@property (nonatomic,strong)NSArray *priceArray;
@property (nonatomic,strong)NSMutableArray *shopperArray;
@property (nonatomic,strong)UICollectionView *priceRangeView;
@property (nonatomic,assign)BOOL isUp;
@property (nonatomic,assign)NSInteger page;
@end

@implementation HSPShopperViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor: [UIColor whiteColor]];
    self.shopperArray = [NSMutableArray array];
    self.priceArray = [[NSArray alloc]initWithObjects:@"≤10",@"10-15",@"15-20",@"20-30",@"30-50",@"≥50", nil];
    [self shopper];
    [self money];
    [self collection];
    [self AlbumaHeader];
     [self AlbumaFooter];
}
- (void)shopper
{
    _tableViewShopper = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 147) style:UITableViewStylePlain];
    _tableViewShopper.delegate =self;
    _tableViewShopper.dataSource = self;
    [self.view addSubview:_tableViewShopper];
}
- (void)money
{
    
}
- (void)collection
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(60, 60);
    flowLayout.minimumInteritemSpacing = 25;
    flowLayout.minimumLineSpacing = 25;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 25);
    
    self.priceRangeView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH - 20, 120) collectionViewLayout:flowLayout];
    self.priceRangeView.backgroundColor = [UIColor grayColor];
    self.priceRangeView.pagingEnabled = YES;
    self.priceRangeView.delegate = self;
    self.priceRangeView.dataSource = self;
    self.tableViewShopper.tableHeaderView = self.priceRangeView;
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, SCREEN_WIDTH, 30)];
    [label setText:@"价格选择区间(单位:万元)"];
    [label setTextColor:[UIColor whiteColor]];
    [label setFont:[UIFont systemFontOfSize:15.]];
    [self.priceRangeView addSubview:label];
    [self.priceRangeView registerClass:[HSPriceCollectionViewCell class] forCellWithReuseIdentifier:@"shoppercell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HSPriceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"shoppercell" forIndexPath:indexPath];
    [cell.labelPrice setText:[self.priceArray objectAtIndex:indexPath.row]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HSPpriceMoneyViewController *price = [[HSPpriceMoneyViewController alloc]init];
    price.titleString = [self.priceArray objectAtIndex:indexPath.row];
    switch (indexPath.row) {
        case 0:
            price.priceUrl = PRICE10;
            break;
        case 1:
            price.priceUrl = PRICE15;
            break;
        case 2:
            price.priceUrl = PRICE20;
            break;
        case 3:
            price.priceUrl = PRICE30;
            break;
        case 4:
            price.priceUrl = PRICE40;
            break;
        case 5:
            price.priceUrl = PRICE50;
            break;
        default:
            break;
    }
    [self.navigationController pushViewController:price animated:YES];
}
#pragma 上拉刷新
- (void)AlbumaHeader
{
    __block HSPShopperViewController *vc = self;
    [_tableViewShopper addLegendHeaderWithRefreshingBlock:^{
        vc.page = 1;
        [vc APPadndatanumber :1];
        _isUp = YES;
    }];
    [_tableViewShopper.header beginRefreshing];
}
#pragma 下拉加载
- (void)AlbumaFooter
{
    __block HSPShopperViewController *vcc = self;
    [_tableViewShopper addLegendFooterWithRefreshingBlock:^{
        vcc.page = vcc.page + 1;
        [vcc APPadndatanumber:vcc.page];
        _isUp = NO;
    }];
}
- (void)APPadndatanumber:(NSInteger)number
{
    if ([Reachability isConnectedInternet] == YES) {
        [AFNBlock getDataWithString:[NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/cms/channels/3?pageNo=%ld&pageSize=20&v=4.0.0",_page] block:^(id back) {
            if (_isUp == YES) {
                [self.shopperArray removeAllObjects];
            }
            for (NSDictionary *dic in [back objectForKey:@"data"]) {
                HSPShopperModel *model = [HSPShopperModel modelWithDictionary:dic];
                [self.shopperArray addObject:model];
            }
            [self.tableViewShopper reloadData];
            [self.tableViewShopper.header endRefreshing];
            [self.tableViewShopper.footer endRefreshing];
        }];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.shopperArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSPFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shopper"];
    if (cell == nil) {
        cell = [[HSPFirstCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"shopper"];
    }
    HSPShopperModel *data = [self.shopperArray objectAtIndex:indexPath.row];
    self.tableViewShopper.scrollsToTop = YES;
    [cell getShopImage:data];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
